package com.dao.zk;

import com.bean.zk.Power;
import com.util.zk.Dbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Repository
public class PowerDAO {
    public List<Power> getAllPowers() {

        Connection con = null;
        QueryRunner qr = new QueryRunner();
        List<Power> powers = null;
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect * from power where power_id != 0\n";
            powers = qr.query(con, sql, new BeanListHandler<>(Power.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return powers;

    }

    /**
     * 获取所有的权限
     *
     * @return
     */
    public List<Power> getPowers() {

        Connection con = null;
        QueryRunner qr = new QueryRunner();
        List<Power> powers = null;
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect p2.power_id,p2.power_name,p.power_name as power_faname,p2.power_remark,p2.belong\t\n" +
                    "\t\tfrom (select power_id ,power_name from power) p ,power p2\n" +
                    "\t\twhere p.power_id = p2.power_rootid";
            powers = qr.query(con, sql, new BeanListHandler<>(Power.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return powers;

    }

    /**
     * 根据父节点和名称模糊查询
     *
     * @return
     */
    public List<Power> getPowersblur(String power_name, Integer power_rootid) {

        Connection con = null;
        QueryRunner qr = new QueryRunner();
        List<Power> powers = null;
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect p2.power_id,p2.power_name,p.power_name as power_faname,p2.power_remark,p2.belong\t\n" +
                    "\t\tfrom (select power_id ,power_name from power) p ,power p2\n" +
                    "\t\twhere p.power_id = p2.power_rootid\n" +
                    "\t\tand p2.power_name like ?" +
                    "\t\tand p2.power_rootid = ?";
            Object[] args = new Object[]{"%" + power_name + "%", power_rootid};
            powers = qr.query(con, sql, new BeanListHandler<>(Power.class), args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return powers;

    }

    public List<Power> getPowersById(int rid) {

        Connection con = null;
        QueryRunner qr = new QueryRunner();
        List<Power> powers = null;
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect power.* from power ,role_power \n" +
                    "\t\twhere rid = ? and power.power_id = role_power.power_id";
            powers = qr.query(con, sql, new BeanListHandler<>(Power.class), rid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return powers;

    }


    /**
     * 查询角色所有权限ID
     *
     * @param rid
     * @return
     */
    public List<Map<String, Object>> getPowerIds(int rid) {
        List<Map<String, Object>> powerIdList = null;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "select power_id from role_power where rid = ?";
            powerIdList = qr.query(con, sql, new MapListHandler(), rid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return powerIdList;
    }

    /**
     * 插入一条数据
     *
     * @param rid     角色id
     * @param powerId 权限id
     * @return 影响行数
     */
    public int insert(int rid, int powerId) {
        int effect = 0;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "insert into role_power values (null,?,?)";
            Object[] args = new Object[]{rid, powerId};
            effect = qr.update(con, sql, args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return effect;
    }

    /**
     * 删除一条数据
     *
     * @param rid     角色id
     * @param powerId 权限id
     * @return 影响行数
     */
    public int delete(int rid, int powerId) {
        int effect = 0;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "delete from role_power where  rid=?  and power_id = ?";
            Object[] args = new Object[]{rid, powerId};
            effect = qr.update(con, sql, args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return effect;
    }

    /**
     * 查询所有的父节点
     *
     * @return
     */
    public List<Power> getFanodes() {
        List<Power> fanodes = null;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect p2.power_name ,p.power_name as faname ,p2.power_id,p2.power_remark,p2.belong\t from (select * from power) p ,power p2\n" +
                    "\t\t where p.power_id = p2.power_rootid\n" +
                    "\t\tand p2.power_rootid = 0 \t\tand p2.power_id!=0\n" +
                    "\t\tGROUP BY p2.power_id";

            fanodes = qr.query(con, sql, new BeanListHandler<>(Power.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return fanodes;
    }

    /**
     * 根据pid获取权限
     *
     * @param parseInt
     * @return
     */
    public List<Power> getByPid(int parseInt) {
        List<Power> fanodes = null;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "\t\tselect p2.power_id,p2.power_name,p.power_name as faname,p2.power_remark,p2.belong\t\n" +
                    "\t\tfrom (select power_id ,power_name from power) p ,power p2\n" +
                    "\t\twhere p.power_id = p2.power_rootid\n" +
                    "\t\tand p2.power_id = ?";
            fanodes = qr.query(con, sql, new BeanListHandler<>(Power.class), parseInt);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return fanodes;
    }

    public int updatePower(Power power) {
        int effect = 0;
        Connection con = null;
        QueryRunner qr = new QueryRunner();
        try {
            con = Dbutil.getConnection();
            String sql = "update power set power_name = ? ,power_remark = ? where power_id = ?";
            Object[] args = new Object[]{power.getPower_name(), power.getPower_remark(), power.getPower_id()};
            effect = qr.update(con, sql, args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            Dbutil.closeAll(null, null, con);
        }
        return effect;
    }
}
